package com.isoftstone.controller;

import cn.hutool.log.Log;
import com.github.pagehelper.PageInfo;
import com.isoftstone.pojo.Dormitory;
//import com.isoftstone.pojo.dto.UpdateDormitoryOccupancyDTO;
import com.isoftstone.pojo.dto.DormitoryOccupancyDTO;
import com.isoftstone.service.DormitoryService;
import com.isoftstone.util.Result;
import jakarta.validation.Valid;
import org.slf4j.ILoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author :W.
 * @Date:2025/9/14 16:39
 */

/**
 * 宿舍信息控制器类
 */
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    private DormitoryService dormitoryService;

    // 获取所有宿舍
    @GetMapping("/findAll")
    public Result<List<Dormitory>> findAll() {
        return dormitoryService.findAll();
    }

    // 分页获取宿舍
    @GetMapping("/findByPage")
    public Result<PageInfo<Dormitory>> findByPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String building_number, // 对应前端buildingNumber
            @RequestParam(required = false) String room_number,     // 对应前端roomNumber
            @RequestParam(required = false) Integer capacity) {
        return dormitoryService.findByPage(currentPage, pageSize, building_number, room_number, capacity);
    }

    // 根据ID获取宿舍
    @GetMapping("/findById/{id}")
    public Result<Dormitory> findById(@PathVariable Integer id) {
        return dormitoryService.findById(id);
    }

    // 新增宿舍
    @PostMapping("/addDormitory")
    public Result<?> addDormitory(@RequestBody Dormitory dormitory) {
        return dormitoryService.addDormitory(dormitory);
    }

    // 更新宿舍
    @PutMapping("/updateDormitory/{id}")
    public Result<?> updateDormitory(
            @PathVariable Integer id,
            @RequestBody Dormitory dormitory) {
        dormitory.setDormitoryId(id); // 确保更新的是路径中指定ID的宿舍
        return dormitoryService.updateDormitory(dormitory);
    }

    // 删除宿舍
    @DeleteMapping("/deleteDormitory/{id}")
    public Result<?> deleteDormitory(@PathVariable Integer id) {
        return dormitoryService.deleteDormitory(id);
    }

    // 查询有空位的宿舍
    @GetMapping("/selectAvailable")
    public Result<List<Dormitory>> selectAvailable() {
        return dormitoryService.selectAvailable();
    }
    // 更新宿舍入住人数
    @PostMapping("/batchDeleteDorms")
    public Result<?> batchDeleteDorms(@RequestBody List<Integer> ids) {
        return dormitoryService.batchDeleteDorms(ids);
    }
    /**
     * 更新入住人数
     */
    @PutMapping("/occupancy")
    public Result<?> updateOccupancy(@RequestBody DormitoryOccupancyDTO dto) {
        // 调用Service层，传递宿舍ID和新的入住人数
        return dormitoryService.updateOccupancy(dto.getDormitoryId(), dto.getCurrentOccupancy());
    }

    // 新增实现：根据ID查询宿舍
    @GetMapping("/getDormitoryById/{dormitoryId}")
    public Result<Dormitory> getDormitoryById(@PathVariable Integer dormitoryId) {
        return dormitoryService.getDormitoryById(dormitoryId);
    }
}
